--[[
    Author          ZhengFeng
    Time            2020.12.07
    Description     宠物主界面-查看部件信息
]]--

require "Common/define"
require "Logic/PetManager"
local Page_PetMainPartInfo = UIBase.New(PanelResNames.Page_PetMainPartInfo)
local this = Page_PetMainPartInfo
local GeneType =
    {
        Genetic = 0,        --遗传
        Mutant = 1,         --突变
        Source = 2,         --初代
    }
local currentPetInfo= nil
local partListIndex = 0
local partListSelected = 1

function this:Init()
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self:RegisterOnClick(self.Btn_BGClose,self.onClickClose)
    self.Btn_Back = self:GetChild("Btn_Back")
    self:RegisterOnClick(self.Btn_Back,self.onClickClose)
    self.Txt_PanelTitle = self:GetChild("PartBG"):GetChild("title")
    self.List_Part = self:GetChild("List_Part")
    self.List_Part.itemRenderer = this.rendererPartListItem

    --组件遗传信息
    self.GeneController = self:GetController("GeneType")
    self.Combo_LeftParent = self:GetChild("Combo_LeftParent")
    self.Txt_LeftParentName = self.Combo_LeftParent:GetChild("Txt_Title")
    self.Img_LeftParentIcon = self.Combo_LeftParent:GetChild("Img_PartIcon")
    self.Combo_RightParent = self:GetChild("Combo_RightParent")
    self.Txt_RightParentName = self.Combo_RightParent:GetChild("Txt_Title")
    self.Img_RightParentIcon = self.Combo_RightParent:GetChild("Img_PartIcon")
    self.Combo_Child = self:GetChild("Combo_Child")
    self.Txt_ChildName = self.Combo_Child:GetChild("Txt_Title")
    self.Img_ChildIcon = self.Combo_Child:GetChild("Img_PartIcon")

end

function this:OnEnable()
   this:showPage()
end

function this:OnDisable()
    self.List_Part.onClickItem:Remove(this.onPartListItemClick)
    currentPetInfo= nil
end
function this:OnDestroy()
    
end

function this:onClickClose()
    closeUI(PanelResNames.Page_PetMainPartInfo)
end

function this:showPage()
    currentPetInfo = PetManager.GetPetInfoByPetId(PetManager.CurrentPetId)
    partListIndex = 0
    partListSelected = 1
    self.List_Part.numItems = #currentPetInfo.genes
    self.List_Part.onClickItem:Add(this.onPartListItemClick)
    
    --"%s的部位信息"
	self.Txt_PanelTitle.text = string.format(getLanguage("PetMain_PartInfo"),currentPetInfo.petName)
    this:showPartGeneticMap()
end
function this:rendererPartListItem(obj)
    partListIndex= partListIndex+1
    local _geneId = currentPetInfo.genes[partListIndex]
    local _geneItem = G.dataTable["t_elvesgene"][tostring(_geneId)]
    local _partName = getLanguage(_geneItem.f_GeneName)
    obj:GetChild("title").text = _partName
    obj.selected = partListSelected == partListIndex

    local _petMutationLevel = _geneItem.f_MutationLevel
    obj:GetController("level"):SetSelectedIndex(_petMutationLevel-1)
end
function this.onPartListItemClick(context)
    local _obj = context.data
    partListSelected = this.List_Part:GetChildIndex(_obj)+1
    
    this:showPartGeneticMap()
end
--更新部件的遗传信息
function this:showPartGeneticMap()
    local _geneConfig = G.dataTable["t_elvesgene"]
    local _childGeneId = currentPetInfo.genes[partListSelected]
    local _childGeneInfo = _geneConfig[tostring(_childGeneId)]
    self.Txt_ChildName.text = getLanguage(_childGeneInfo.f_GeneName)
    self.Img_ChildIcon.url = PetManager.GetPetIconByGeneInfo(_childGeneInfo)
    local _petMutationLevel = _childGeneInfo.f_MutationLevel
    self.Combo_Child:GetController("level"):SetSelectedIndex(_petMutationLevel-1)

    local _selectedIndex = GeneType.Source
    if #currentPetInfo.geneticMap >= 2 then
        local _leftGeneId = currentPetInfo.geneticMap[PetManager.FamilyTree.Father].genes[partListSelected]
        local _leftGeneInfo = _geneConfig[tostring(_leftGeneId)]
        self.Txt_LeftParentName.text = getLanguage(_leftGeneInfo.f_GeneName)
        self.Img_LeftParentIcon.url = PetManager.GetPetIconByGeneInfo(_leftGeneInfo)
        _petMutationLevel = _leftGeneInfo.f_MutationLevel
        self.Combo_LeftParent:GetController("level"):SetSelectedIndex(_petMutationLevel-1)

        local _rightGeneId = currentPetInfo.geneticMap[PetManager.FamilyTree.Mother].genes[partListSelected]
        local _rightGeneInfo = _geneConfig[tostring(_rightGeneId)]
        self.Txt_RightParentName.text = getLanguage(_rightGeneInfo.f_GeneName)
        self.Img_RightParentIcon.url = PetManager.GetPetIconByGeneInfo(_rightGeneInfo)
        _petMutationLevel = _rightGeneInfo.f_MutationLevel
        self.Combo_RightParent:GetController("level"):SetSelectedIndex(_petMutationLevel-1)

        if _childGeneId == _leftGeneId or _childGeneId == _rightGeneId then
            _selectedIndex = GeneType.Genetic
        else
            _selectedIndex = GeneType.Mutant
        end
    end

    self.GeneController:SetSelectedIndex(_selectedIndex)
end

return this